package cn.ysym.dao;

import android.content.Context;

import java.util.ArrayList;
import java.util.List;

import cn.ysym.model.Dealer;

/**
 * Created by Administrator on 2016/12/19 0019.
 */

public class DealerDao extends DaoBase {

    public DealerDao(Context context) {
        super(context);
    }

    public Dealer queryById(String dealerId) {
        List<String[]> objs = this.query(Integer.MAX_VALUE, "select dealerId, dealerName, dealerNamePy from dealer where dealerId = ?", dealerId);
        if (objs.size() > 0) {
            String[] row = objs.get(0);
            Dealer dealer = new Dealer();
            dealer.setDealerId(row[0]);
            dealer.setDealerName(row[1]);
            dealer.setDealerNamePy(row[2]);
            return dealer;
        }

        return null;
    }

    public List<Dealer> query(String filterStr) {
        List<Dealer> ret = new ArrayList<>();
        List<String[]> objs;
        if (filterStr != null && filterStr.length() > 0)
            objs = this.query(Integer.MAX_VALUE, "select dealerId, dealerName, dealerNamePy from dealer where dealerId like '%" + filterStr + "%' or dealerName like '%" + filterStr + "%' or dealerNamePy like '%" + filterStr + "%' order by dealerName");
        else
            objs = this.query(Integer.MAX_VALUE, "select dealerId, dealerName, dealerNamePy from dealer order by dealerName");
        for (String[] row : objs) {
            Dealer dealer = new Dealer();
            dealer.setDealerId(row[0]);
            dealer.setDealerName(row[1]);
            dealer.setDealerNamePy(row[2]);
            ret.add(dealer);
        }
        return ret;
    }

    public void insert(Dealer model) {
        this.execSql("insert into dealer(dealerId, dealerName, dealerNamePy) values(?, ?, ?)", model.getDealerId(), model.getDealerName(), model.getDealerNamePy());
    }

    public void clearAll() {
        this.execSql("delete from dealer");
    }
}
